package com.xiaoshuidi.cloud.module.finance.controller.app.order.vo;

import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** 用户 APP - 支付订单提交 Request VO */
@Schema(description = "用户 APP - 支付订单提交 Request VO")
@Data
public class AppPayOrderSubmitReqVO {
    /**
     * 支付订单来源id(账单/预约单等)
     */
    @NotEmpty(message = "支付订单来源id不能为空")
    private String sourceBizId;
    /**
     * 支付订单来源类型(账单/预约单等)
     * {@link com.xiaoshuidi.cloud.module.finance.enums.finance.SourceBizTypeEnum}
     */
    @NotEmpty(message = "支付订单来源类型不能为空")
    private String sourceBizType;
    /**
     * 应用id
     */
    @NotNull(message = "支付应用id不能为空")
    private Long appId;
    /**
     * 支付渠道
     * @see com.xiaoshuidi.cloud.framework.pay.core.enums.PayChannelEnum
     */
    @NotEmpty(message = "支付渠道不能为空")
    private String channelCode;
    /**
     * 支付渠道所需参数
     */
    private Map<String, String> channelExtras;
    /**
     * 优惠券id
     */
    private Object couponId;

    private Long discount;

    private String displayMode;

    private Set<Long> couponIds;

    /**
     * 支付金额
     */
    private BigDecimal price;
    public Set<Long> getCouponIds() {
        if (couponIds != null) {
            return couponIds;
        }
        if (couponId != null && StringUtils.hasText(couponId.toString())) {
            if (couponId.toString().startsWith("[") && couponId.toString().endsWith("]")) {
                couponIds = new HashSet<>(JsonUtils.parseArray(couponId.toString(), Long.class));
            } else {
                couponIds = Stream.of(couponId.toString().split(",")).map(Long::parseLong).collect(Collectors.toSet());
            }
        }
        return couponIds;
    }
}
